﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using AdminModule.IndexMarkServiceReference;
using SharedCode;
using AdminModule.UserControls.StructView;
using AdminModule.ViewModels;
using AdminModule.CommonClasses;

namespace AdminModule.Views
{
    public partial class Main : Page
    {
        private MainVM viewModel;
        private AdmTerm term;

        public Main()
        {
            InitializeComponent();
            viewModel = Resources["mainVM"] as MainVM;
            viewModel.LoadCompleted += new SharedCode.ViewModelBase.LoadEventHandler(viewModel_LoadCompleted);
            //MessageBox.Show("1");
        }

        void viewModel_LoadCompleted(object sender, LoadEventArgs e)
        {
            this.progressBar.IsVisibility = Visibility.Collapsed;
            //MessageBox.Show(termsComboBox.Items.Count.ToString());

            if(e.Type == typeof(AdmTerm))
            {
                if (termsComboBox.Items.Count > 0)
                {
                    //termsComboBox.SelectedIndex = 3;
                    AdmTerm tmp = viewModel.Terms.First(x => x.Stop.Year == DateTime.Now.Year);
                    if (tmp != null)
                        termsComboBox.SelectedItem = tmp;
                    else
                        termsComboBox.SelectedItem = viewModel.Terms.Single(x => x.Stop.Year == viewModel.Terms.Max(y => y.Stop.Year));
                }
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void departmentsLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo( new StructView(StructName.University, term.IDTerm));
        }

        private void categoriesLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmCategoriesByTerm, term.IDTerm, new EditCategoryWeightVM()));
        }

        private void positionsLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmPosition, null, null));
        }

        private void statusesLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmStatus, null, null));
        }

        private void jobsLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmJob, null, new EditJobsVM()));
        }

        private void personsLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmPerson, null, null));
        }

        private void termsLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmTerm, null, null));
        }

        private void unitsLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmUnit, null, null));
        }

        private void termsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            term = termsComboBox.SelectedItem as AdmTerm;
        }

        private void indexMarkStructLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new StructView(StructName.IndexMark, term.IDTerm));
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            navigationTreeView.ExpandToDepth(2);
            this.progressBar.IsVisibility = Visibility.Visible;
            viewModel.LoadTerms();
        }

        private void gradationsStructLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new StructView(StructName.Gradations, term.IDTerm));
        }

        private void characteristicsLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmCharacteristic, null, null));
        }

        private void departemntsLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmDepartment, null, new EditDepartmentVM()));
        }

        private void sectionsLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmSection, null, null));
        }

        private void responsiblesLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmResponsible, null, new EditResponsiblesVM()));
        }

        private void gradationstLink_Click_1(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmGradation, null, null));
        }

        private void categoriesLink_Click_1(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmCategory, null, null));
        }

        /// <summary>
        /// Осуществляет переход на страницу. Предварительно проверяет наличие не сохраненных элементов.
        /// </summary>
        /// <param name="target"></param>
        private void NavigateTo(object target)
        {
            if (StructViewBase.changedElements.Count > 0 && AdminContentFrame.Content is StructView)
            {
                MessageBoxResult r = MessageBox.Show("Для продолжения необходимо сохранить изменения. Сохранить изменения?", MyAssemblyInfo.Title, MessageBoxButton.OKCancel);
                if (r == MessageBoxResult.OK)
                {
                    (AdminContentFrame.Content as StructView).Save();
                    return;
                }
                else
                {
                    StructViewBase.changedElements.Clear();
                }
            }

            AdminContentFrame.Content = target;
        }

        private void answerLogLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewAnswerLog(LogName.Answer));
        }

        private void inputLogLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewAnswerLog(LogName.Input));
        }

        private void departemntTypesLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmDepartmentType, null, null));
        }

        private void degreesLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmDegree, null, null));
        }

        private void personDegreetLink_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewData(ViewName.AdmPersonDegree, term.IDTerm, new EditPersonDegreeVM()));
        }

        private void _ImportAnket_Click(object sender, RoutedEventArgs e)
        {
            NavigateTo(new ViewImport());
        }
    }
}
